package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.datasource.entities.*;
import com.jsh.erp.service.crk.KucunService;
import com.jsh.erp.service.crk.ShipuDayService;
import com.jsh.erp.service.crk.ShipuService;
import com.jsh.erp.service.crk.ShipuWeekService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.ErpInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;

/**
 * @author jishenghua  jshERP 2018年12月25日14:38:08
 */
@RestController
@RequestMapping(value = "/shipu")
@Api(tags = {"食谱"})
public class ShipuController {
    private Logger logger = LoggerFactory.getLogger(ShipuController.class);

    @Resource
    private ShipuService shipuService;
    @Resource
    private ShipuDayService shipuDayService;
    @Resource
    private ShipuWeekService shipuWeekService;
    @Resource
    private KucunService kucunService;

    @GetMapping(value = "/findAll")
    @ApiOperation(value = "查找食谱信息")
    public BaseResponseInfo findBySelect(HttpServletRequest request) throws Exception{
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            List<Shipu> dataList = shipuService.findBySelect();
            JSONArray arr = new JSONArray();
            for (Shipu shipu : dataList) {
                JSONObject item = new JSONObject();
                item.put("id", shipu.getId());
                item.put("name", shipu.getName());
                item.put("type", shipu.getType());
                arr.add(item);
            }
            res.code = 200;
            res.data = arr;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "获取数据失败";
        }
        return res;
    }

    @GetMapping(value = "/day/findAll")
    @ApiOperation(value = "查找食谱信息(天)")
    public BaseResponseInfo findShipuDayAll(HttpServletRequest request) throws Exception{
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            List<String> dataList = shipuService.queryDayList();
            JSONArray arr = new JSONArray();
            for (String day : dataList) {
                JSONObject item = new JSONObject();
                item.put("day", day);
                arr.add(item);
            }
            res.code = 200;
            res.data = arr;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "获取数据失败";
        }
        return res;
    }

    /**
     * 批量设置状态-启用或者禁用
     * @param jsonObject
     * @param request
     * @return
     */
    @PostMapping(value = "/batchSetStatus")
    @ApiOperation(value = "批量设置状态")
    public String batchSetStatus(@RequestBody JSONObject jsonObject,
                                 HttpServletRequest request)throws Exception {
        Boolean status = jsonObject.getBoolean("status");
        String ids = jsonObject.getString("ids");
        Map<String, Object> objectMap = new HashMap<>();
        int res = shipuService.batchSetStatus(status, ids);
        if(res > 0) {
            return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
        } else {
            return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
        }
    }

    @PostMapping(value = "/addDayDetail")
    public Object addDayDetail(@RequestBody ShipuDayHeadVo4Body body, HttpServletRequest request) throws  Exception{
        JSONObject result = ExceptionConstants.standardSuccess();
        shipuService.addDayDetail(body.getInfo(), body.getRows());
        return result;
    }

    @GetMapping(value = "/day/list")
    @ApiOperation(value = "查找食谱(天)信息")
    public BaseResponseInfo listByDay(HttpServletRequest request) throws Exception{
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            List<Shipu> dataList = shipuService.findBySelect();
            JSONArray arr = new JSONArray();
            for (Shipu shipu : dataList) {
                JSONObject item = new JSONObject();
                item.put("id", shipu.getId());
                item.put("name", shipu.getName());
                item.put("type", shipu.getType());
                arr.add(item);
            }
            res.code = 200;
            res.data = arr;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "获取数据失败";
        }
        return res;
    }

    /**
     * @param day
     * @return
     */
    @GetMapping(value = "/day/getDetailList")
    @ApiOperation(value = "查询信息")
    public BaseResponseInfo getDayDetailList(@RequestParam("day") String day)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            List<ShipuDay> list = shipuService.getDayDetailList(day);
            res.code = 200;
            res.data = list;
        } catch(Exception e){
            e.printStackTrace();
            res.code = 500;
            res.data = "获取数据失败";
        }
        return res;
    }
    @GetMapping(value = "/day/getShipuList")
    @ApiOperation(value = "查询信息")
    public BaseResponseInfo getShipuList(@RequestParam("day") String day)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            List<ShipuDay> list = shipuService.queryShipuListByDay(day);
            res.code = 200;
            res.data = list;
        } catch(Exception e){
            e.printStackTrace();
            res.code = 500;
            res.data = "获取数据失败";
        }
        return res;
    }

    @DeleteMapping(value = "/day")
    public BaseResponseInfo deleteDayDetail(@RequestParam("day") String day) throws  Exception{
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            shipuService.deleteDayDetail(day);
            res.code = 200;
        } catch(Exception e){
            e.printStackTrace();
            res.code = 500;
            res.data = "获取数据失败";
        }
        return res;
    }

    @PostMapping(value = "/addWeekDetail")
    public Object addWeekDetail(@RequestBody ShipuWeekHeadVo4Body body, HttpServletRequest request) throws  Exception{
        JSONObject result = ExceptionConstants.standardSuccess();
        shipuWeekService.addDetail("add", body.getInfo(), body.getRows());
        return result;
    }

    @PutMapping(value = "/updateWeekDetail")
    public Object updateWeekDetail(@RequestBody ShipuWeekHeadVo4Body body, HttpServletRequest request) throws  Exception{
        JSONObject result = ExceptionConstants.standardSuccess();
        shipuWeekService.addDetail("edit", body.getInfo(), body.getRows());
        return result;
    }

    /**
     * @param startDate
     * @return
     */
    @GetMapping(value = "/week/getDetailList")
    @ApiOperation(value = "查询信息")
    public BaseResponseInfo getWeekDetailList(@RequestParam("startDate") String startDate)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            List<ShipuWeek> list = shipuWeekService.getDetailList(startDate);
            res.code = 200;
            res.data = list;
        } catch(Exception e){
            e.printStackTrace();
            res.code = 500;
            res.data = "获取数据失败";
        }
        return res;
    }

    @DeleteMapping(value = "/week")
    public BaseResponseInfo deleteWeek(@RequestParam("day") String day) throws  Exception{
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            shipuWeekService.deleteDetail(day);
            res.code = 200;
        } catch(Exception e){
            e.printStackTrace();
            res.code = 500;
            res.data = "获取数据失败";
        }
        return res;
    }
}
